package com.aidex.biz.service.impl;

import com.aidex.biz.domain.Taizhang007;
import com.aidex.biz.mapper.Taizhang007Mapper;
import com.aidex.biz.service.Taizhang007Service;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.exception.CustomException;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 廉政谈话台账Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-05-22
 */
@Service
@Transactional(readOnly = true)
public class Taizhang007ServiceImpl extends BaseServiceImpl<Taizhang007Mapper, Taizhang007> implements Taizhang007Service {
    private static final Logger log = LoggerFactory.getLogger(Taizhang007ServiceImpl.class);

    /**
     * 获取单条数据
     *
     * @param taizhang007 廉政谈话台账
     * @return 廉政谈话台账
     */
    @Override
    public Taizhang007 get(Taizhang007 taizhang007) {
        Taizhang007 dto = super.get(taizhang007);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 廉政谈话台账id
     * @return 廉政谈话台账
     */
    @Override
    public Taizhang007 get(String id) {
        Taizhang007 dto = super.get(id);
        return dto;
    }


    /**
     * 查询廉政谈话台账列表
     *
     * @param taizhang007 廉政谈话台账
     * @return 廉政谈话台账
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<Taizhang007> findList(Taizhang007 taizhang007) {
        return super.findList(taizhang007);
    }

    /**
     * 分页查询廉政谈话台账列表
     *
     * @param taizhang007 廉政谈话台账
     * @return 廉政谈话台账
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<Taizhang007> findPage(Taizhang007 taizhang007) {
        return super.findPage(taizhang007);
    }

    /**
     * 保存廉政谈话台账
     *
     * @param taizhang007
     * @return 结果
     */
    @Override
    public boolean save(Taizhang007 taizhang007) {
        return super.save(taizhang007);
    }


    @Override
    @Transactional(readOnly = false)
    public String importData(List<Taizhang007> taizhang007s) {
        if (StringUtils.isNull(taizhang007s) || taizhang007s.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Taizhang007 obj : taizhang007s) {
            try {
                if (StringUtils.isNotNull(obj.getTz00701())) {
                    obj.setDeptId(SecurityUtils.getDeptId());
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy",
                            Locale.ENGLISH).withZone(ZoneId.of("America" + "/Chicago"));
                    // 解析字符串为LocalDateTime
                    LocalDateTime dateTime = LocalDateTime.parse(obj.getTz00701(), formatter);

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
                    obj.setTz00701(sdf.format(date));
                    this.save(obj);
                    successNum++;
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "";
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确。");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "数据已全部导入成功！共 " + successNum + " 条。：");
        }
        return successMsg.toString();

    }

    /**
     * 删除廉政谈话台账信息
     *
     * @param taizhang007
     * @return 结果
     */
    @Override
    public boolean remove(Taizhang007 taizhang007) {
        return super.remove(taizhang007);
    }

    /**
     * 批量删除廉政谈话台账
     *
     * @param ids 需要删除的廉政谈话台账ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteTaizhang007ByIds(String[] ids) {
        return mapper.deleteTaizhang007ByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param taizhang007 廉政谈话台账
     * @return 结果
     */
    @Override
    public int findMaxSort(Taizhang007 taizhang007) {
        return NumberUtils.nextOrder(mapper.findMaxSort(taizhang007));
    }
}
